// ... existing code ...
<script setup>
import { RouterView, useRoute } from 'vue-router'
import { computed, ref, watch, onMounted } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import ErrorHandler from '@/components/ErrorHandler.vue'

const store = useStore()
const router = useRouter()
const route = useRoute()
const isAuthenticated = computed(() => store.getters.isAuthenticated)
const isAdmin = computed(() => store.getters.isAdmin)
const cartItemCount = computed(() => store.getters.cartItemCount)
const activeIndex = ref('/')

const updateActiveIndex = () => {
  if (route.path.startsWith('/medicines')) {
    activeIndex.value = '/medicines'
  } else if (route.path.startsWith('/orders')) {
    activeIndex.value = '/orders'
  } else if (route.path.startsWith('/health')) {
    activeIndex.value = '/health'
  } else if (route.path.startsWith('/consultation')) {
    activeIndex.value = '/consultation'
  } else if (route.path.startsWith('/admin')) {
    activeIndex.value = '/admin'
  } else {
    activeIndex.value = route.path
  }
}

watch(() => route.path, () => {
  updateActiveIndex()
})

onMounted(() => {
  updateActiveIndex()
  window.addEventListener('beforeunload', () => {
    localStorage.setItem('lastRoute', route.fullPath)
  })
})

const logout = () => {
  localStorage.removeItem('token')
  localStorage.removeItem('role')
  localStorage.removeItem('username')
  localStorage.removeItem('userId')
  localStorage.removeItem('cart')
  store.commit('SET_USER', null)
  store.commit('CLEAR_CART')
  router.push('/login')
}

const navigate = (path) => {
  activeIndex.value = path
  router.push(path)
}
</script>

<template>
  <div id="app">
    <ErrorHandler />
    <div class="home">
      <template v-if="!route.meta.hideLayout">
      <div class="top-header">
        <div class="top-header-content">
          <div class="contact-info">
            <span style="font-size:16px;margin-right:6px;">📞</span>
            <span>24小时服务热线：400-123-4567</span>
          </div>
          <div class="user-actions">
            <template v-if="!isAuthenticated">
              <button class="link-btn" @click="$router.push('/login')">登录</button>
              <span class="divider"></span>
              <button class="link-btn" @click="$router.push('/register')">注册</button>
            </template>
            <template v-else>
              <button class="link-btn" @click="$router.push('/cart')">购物车<span v-if="cartItemCount">({{ cartItemCount }})</span></button>
              <span class="divider"></span>
              <button class="link-btn" @click="$router.push('/profile')">个人中心</button>
              <span class="divider"></span>
              <button class="link-btn" @click="logout">退出</button>
            </template>
          </div>
        </div>
      </div>
      <header class="custom-header">
        <div class="header-content">
          <div class="logo" @click="$router.push('/')">
            <img src="/src/assets/logo.png" alt="梓桐健康医药网" class="logo-image" />
            <span class="logo-text">梓桐健康医药网</span>
          </div>
          <nav class="nav-links">
            <a :class="{active: activeIndex === '/'}" @click.prevent="navigate('/')">首页</a>
            <a :class="{active: activeIndex === '/medicines'}" @click.prevent="navigate('/medicines')">药品商城</a>
            <a :class="{active: activeIndex === '/health'}" @click.prevent="navigate('/health')">养生知识</a>
            <a :class="{active: activeIndex === '/consultation'}" @click.prevent="navigate('/consultation')">健康咨询</a>
            <a v-if="isAdmin" :class="{active: activeIndex === '/admin'}" @click.prevent="navigate('/admin')">管理后台</a>
          </nav>
        </div>
      </header>
      </template>
      <main>
        <div class="main-content" :class="{ 'full-page': route.meta.hideLayout }">
          <RouterView />
        </div>
      </main>
      <div class="global-footer" v-if="!route.path.startsWith('/admin')">© 2025 梓桐健康医药网 版权所有</div>
    </div>
  </div>
</template>

<style scoped>
.home {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  padding-bottom: 60px;
}

.top-header {
  background-color: #f0f9ff;
  padding: 8px 0;
  border-bottom: 1px solid #e6f4ff;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  z-index: 998;
}

.top-header-content {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  font-size: 13px;
}

.contact-info {
  display: flex;
  align-items: center;
  color: #0081ff;
}

.custom-header {
  background: #fff;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
  position: fixed;
  top: 40px; /* .top-header高度 */
  left: 0;
  width: 100%;
  z-index: 1000;
  height: 70px;
  display: flex;
  align-items: center;
}

.header-content {
  max-width: 1200px;
  margin: 0 auto;
  height: 100%;
  display: flex;
  align-items: center;
  padding: 0 20px;
  width: 100%;
  justify-content: space-between;
}

.logo {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 0 20px;
}

.logo-image {
  height: 40px;
  margin-right: 10px;
}

.logo-text {
  font-size: 20px;
  font-weight: bold;
  color: #409EFF;
}

.nav-links {
  display: flex;
  gap: 30px;
}

.nav-links a {
  font-size: 16px;
  color: #1a1a1a;
  font-weight: 500;
  text-decoration: none;
  padding: 0 10px;
  line-height: 70px;
  transition: color 0.2s;
  position: relative;
}

.nav-links a.active,
.nav-links a:hover {
  color: #0081ff;
}

.nav-links a.active::after {
  content: '';
  position: absolute;
  bottom: 10px;
  left: 10px;
  right: 10px;
  height: 3px;
  background: #0081ff;
  border-radius: 2px;
}

.user-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.link-btn {
  background: none;
  border: none;
  color: #409EFF;
  font-size: 13px;
  cursor: pointer;
  padding: 0 8px;
}

.link-btn:hover {
  color: #0081ff;
  text-decoration: underline;
}

.divider {
  display: inline-block;
  width: 1px;
  height: 14px;
  background: #e6e6e6;
  margin: 0 8px;
}

.main-content {
  margin-top: 130px; /* 40px + 70px，避免被header遮挡 */
  padding: 0 20px;
  max-width: 1200px;
  margin-left: auto;
  margin-right: auto;
  padding-bottom: 60px; /* 为底部导航留出空间 */
}

.main-content.full-page {
  margin: 0;
  padding: 0;
  max-width: none;
}

.main-content.full-page :deep(.login-container),
.main-content.full-page :deep(.register-container) {
  height: 100vh;
  margin: 0;
  padding: 0;
}

.global-footer {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 36px;
  background: #e6f7ff;
  color: #303133;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  z-index: 9999;
  box-shadow: 0 -1px 6px rgba(0,0,0,0.04);
}
</style>
